{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "129c1a09",
   "metadata": {},
   "source": [
    " # 1.本周内容\n",
    " \n",
    " ## [集合] (https://docs.python.org/3/tutorial/datastructures.html#sets)\n",
    " \n",
    " > 1.集合的性质\n",
    " >> 1.无序\n",
    " >> 2.不重复\n",
    " > 2.集合的一般方法\n",
    " >> 1.增加\n",
    " >> 2.删除\n",
    " >> 3.查询：in 查询成员关系\n",
    " > 3.集合的数学逻辑运算\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "86faa92e",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_duplicate_age = []\n",
    "list_age = [18,24,25,24,18,23,18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "53f6dc7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 24, 25, 23]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9119fe12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{24, 25, 18, 23}\n"
     ]
    }
   ],
   "source": [
    "# list ---> set 的方法: set()\n",
    "no_duplicate_age_set = set(list_age)\n",
    "print(no_duplicate_age_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6b0fdf53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'orange', 'apple', 'pear', 'orange', 'banana')\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方测试文档\n",
    "# 1.移除重复项\n",
    "basket = ('apple','orange','apple','pear','orange','banana')\n",
    "print(basket)\n",
    "# 2.in查询成员关系\n",
    "print('orange' in basket)\n",
    "print('crabgrass' in basket)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b4449f0",
   "metadata": {},
   "source": [
    "* 集合的创建的两种一般方法\n",
    "> 1.set(list)\n",
    "> 2.直接按照集合的结构创建：{value1，value2，value3，...}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0836183",
   "metadata": {},
   "source": [
    "> 1.增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0f4e4a0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7cbf82b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4e8116a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 99, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(99)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05970231",
   "metadata": {},
   "source": [
    "> 2.删除\n",
    ">> 1.pop()\n",
    ">> 2.remove()\n",
    ">> 3.discard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c0abfe9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "79a275d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.set instance\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "be702286",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n",
      "{0, 1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.Remove and return an arbitrary set element.\n",
    "print(set_1)\n",
    "set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9700e2e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *避免keyerror的方式：\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8d5098fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7e7d0c95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function discard:\n",
      "\n",
      "discard(...) method of builtins.set instance\n",
      "    Remove an element from a set if it is a member.\n",
      "    \n",
      "    If the element is not a member, do nothing.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.discard)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a24957",
   "metadata": {},
   "source": [
    "### 3.集合的运算\n",
    "\n",
    "> 1.集合的交集\n",
    ">> 1.使用集合的方法：set_1.intersection(set_2)\n",
    ">> 2.set_1&set_2\n",
    "> 2.集合的并集\n",
    "\n",
    "> 3.集合的差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6f4da8d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "845805ac",
   "metadata": {},
   "source": [
    "> 1.集合的交集\n",
    ">> 1.使用集合的方法：set_1.intersection(set_2)\n",
    ">> 2.使用逻辑运算符：set_1 & set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f9c06563",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function intersection:\n",
      "\n",
      "intersection(...) method of builtins.set instance\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5a3014bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用集合的方法：set_1.intersection(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7f3017c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符：set_1 & set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 & set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0cf97037",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：\n",
    "set_1_xu = {\"星期一12-14\",\"星期二3-5\",\"星期二12-14\" }\n",
    "set_2_前端 = {\"星期二3-5\",\"星期二12-14\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "32a13320",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'星期二12-14', '星期二3-5'}\n",
      "结论：周一班 API课 让给前端的同学~\n"
     ]
    }
   ],
   "source": [
    "print(set_1_xu & set_2_前端)\n",
    "print(\"结论：周一班 API课 让给前端的同学~\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcfdee50",
   "metadata": {},
   "source": [
    "> 1.集合的并集\n",
    ">> 1.使用并集的方法：set_1.union(set_2)\n",
    ">> 2.使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "934792a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用并集的方法：set_1.union(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "48d3b137",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 2. 使用逻辑运算符：set_1 | set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3cd63e38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间 :  1639362794.6208642\n",
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "结束的时间 :  1639362794.6228642\n",
      "运行的时间 :  0.002000093460083008\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 = 运行后的时间节点 - 运行前的时间节点  : 思考并对比列表运算的执行效率 \n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间 : \",start_time)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间 : \",stop_time)\n",
    "print(\"运行的时间 : \", stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c78a119d",
   "metadata": {},
   "source": [
    "> 3.集合的差集（考虑先后顺序）\n",
    ">> 1.使用差集的方法：set_1.difference(set_2)\n",
    ">> 2.使用逻辑运算符：set_1 - set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "94e40779",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1 和 set_2 差集结果 :  {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "438e4ad1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2 和 set_1 差集结果 :  {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2.difference(set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b5ff10e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1 和 set_2 差集结果 :  {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1 - set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f7bf55b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2 和 set_1 差集结果 :  {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2 - set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0a609dd",
   "metadata": {},
   "source": [
    "> 4.对称差集\n",
    ">> 1.用对称差集的方法：set_1.symmetric_difference(set_2)\n",
    ">> 2.使用逻辑运算符：? (set_1 - set_2) | (set_2-set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4c96c4d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.symmetric_difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d772e977",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果 :  {0, 66, 2, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",set_1.symmetric_difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6e48520e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果 :  {0, 2, 66, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",(set_1 - set_2) | (set_2-set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "880c3667",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:aeiou\n",
      "a\n",
      "u\n",
      "o\n",
      "i\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "#集合的实践\n",
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:          \n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "bab48486",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels :  {'a', 'u', 'o', 'i', 'e'}\n",
      "Provide a word to search for vowels:vowels\n",
      "word :  {'e', 'w', 's', 'o', 'l', 'v'}\n",
      "{'o', 'e'}\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "print(\"vowels : \",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word : \",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71a363b8",
   "metadata": {},
   "source": [
    "## 课后作业：\n",
    "\n",
    "课堂练习\n",
    "\n",
    "课本第三章 课后项目练习\n",
    "\n",
    "总结集合方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ee9575",
   "metadata": {},
   "source": [
    "# 总结\n",
    ">集合的方法：1.集合的两种一般方法（1.set(list)\n",
    "2.直接按照集合的结构创建：{value1,value2,value3,...}）\n",
    ">集合的运算：并集 交集 差集(逻辑运算符）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2bcd6c0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
